main.js
//"@highlightjs/vue-plugin": "^2.1.0",
//"highlight.js": "^11.11.1",
//"github-markdown-css": "^5.8.1",
//"markdown-it": "^14.1.0",
import 'github-markdown-css'
import 'highlight.js/styles/stackoverflow-light.css'
import hljs from "highlight.js/lib/core";
import hljsVuePlugin from "@highlightjs/vue-plugin";
//import "highlight.js/lib/common"; //单一加载
//按需引入语言
import javascript from "highlight.js/lib/languages/javascript";
import java from "highlight.js/lib/languages/java";
import sql from "highlight.js/lib/languages/sql";
import xml from "highlight.js/lib/languages/xml";
import html from "highlight.js/lib/languages/vbscript-html";
import json from "highlight.js/lib/languages/json";
import yaml from "highlight.js/lib/languages/json";
hljs.registerLanguage("javascript", javascript);
hljs.registerLanguage("java", java);
hljs.registerLanguage("yaml", yaml);
hljs.registerLanguage("json", json);
hljs.registerLanguage("sql", sql);
hljs.registerLanguage("xml", xml);
hljs.registerLanguage("html", html);
...
app.use(hljsVuePlugin)

<template>
    <div class="ai_container">
        <el-dialog v-model="dialogVisible" width="700" :show-close="false">
            <div class="container" v-if="userStore.websocketFlag">
                <Header>
                    <template v-slot:title>
                        <div style="font-size: 15px;">ai问答</div>
                    </template>
                </Header>
                <div class="speak" ref="speakDOM">
                    <a href="#D" ref="A_DOM" style="display: none">锚点</a>
                    <div class="item" v-for="(item, index) in dialogue" :key="index">
                        <div :class="{
                            content: true,
                            'markdown-body': true,
                            ai: item.name === 'ai',
                            user: item.name === 'user',
                        }" v-loading="item.loading" v-html="item.content">
                        </div>
                        <div class="content_button" v-show="item.loading">
                            <el-button plain @click="cancel(index)">取消思考</el-button>
                        </div>
                    </div>
                    <div style="height: 30px" id="D"></div>
                </div>
                <div class="input">
                    <div class="left">
                        <el-input type="textarea" :disabled="textareaDisabled" v-model="text" resize="none"
                            @keydown.enter="send"></el-input>
                    </div>
                    <div class="right">
                        <el-button icon="Position" size="large" color="#2ecacf" @click="send"></el-button>
                    </div>
                </div>
            </div>
            <div v-else>
                <el-empty description="websocket连接中~，请等待连接完成" />
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, defineExpose, watch, onMounted } from 'vue'
import MarkdownIt from 'markdown-it'
import hljs from "highlight.js/lib/core";
import Header from '@/components/Ai/Header.vue'
import { ai } from '@/api/ai/index.js'
import { ElMessage } from 'element-plus'
import mitter from '@/utils/eventBus.js'
import useUserStore from '@/store/modules/user.js'
const userStore = useUserStore()
const md = new MarkdownIt({
    html: true,
    linkify: true,
    typographer: true,
    highlight: function (str, lang) {
        if (lang && hljs.getLanguage(lang)) {
            try {
                return hljs.highlight(lang, str, true).value;
            } catch (__) { }
        }
        return ''; // 使用额外的默认转义
    }
})
const dialogue = ref([
    // {
    //     name: 'ai',
    //     content: ``,
    //     loading: false,
    // },
    //  {
    //     name: 'user',
    //     content: '你也好',
    //     loading: false,
    // }
])
const textareaDisabled = ref(false)
const dialogVisible = ref(false)
watch(dialogVisible, (newVal, oldVal) => {
    if (newVal) {
        dialogue.value.push({
            name: 'ai',
            content: ``,
            loading: false,
        })
        mitter.emit('socketSend', {
            type: "welcome",
            msg: '',
            index: dialogue.value.length - 1
        })
        return
    }
    else {
        dialogue.value = []
        // textareaDisabled.value = false
    }
})
const text = ref('')
const A_DOM = ref(null)
const setDialogVisible = (flag) => {
    dialogVisible.value = flag
}
//发送消息========================start
const send = () => {
    if (text.value.trim().length === 0) return ElMessage.warning('请输入')
    dialogue.value.push(
        {
            name: 'user',
            content: text.value.trim(),
            loading: false,
        }
    )
    dialogue.value.push(
        {
            name: 'ai',
            content: '<div>ai</div><div>正在思考中~~~~~</div>',
            loading: true,
        }
    )
    mitter.emit('socketSend', {
        type: "aiQuestion",
        msg: text.value.trim(),
        index: dialogue.value.length - 1
    })
    textareaDisabled.value = true
    A_DOM.value.click()
    text.value = ''
}
const formAiMessage = ({ message, index }) => {
    if (dialogue.value[index].content === '已取消思考') return
    textareaDisabled.value = false
    dialogue.value[index].loading = false
    dialogue.value[index].content = md.render(message)
    A_DOM.value.click()
}
//发送消息========================end


//取消思考========================start
const cancel = (index) => {
    textareaDisabled.value = false
    dialogue.value[dialogue.value.length - 1].loading = false
    dialogue.value[dialogue.value.length - 1].content = '已取消思考'
}
//取消思考========================end
onMounted(() => {
    mitter.on('formAiMessage', formAiMessage)
})
defineExpose({ setDialogVisible })
</script>
<style scoped lang="scss">
.ai_container {

    .container {
        padding: 0 0 10px 0;
        height: 90vh;
        position: relative;
        overflow: hidden;
        background-color: #f9f9f9;

        >.speak {
            overflow-y: auto;
            height: calc(100% - 150px);

            >.item {
                padding: 30px 10px;
                display: flex;
                flex-direction: column;
                row-gap: 10px;
                position: relative;

                >.content {
                    line-height: 25px;
                    display: inline-block;
                    max-width: 70%;
                    padding: 10px;
                    border-radius: 10px;
                    box-shadow: 0 0 5px rgba(102, 102, 102, .5);
                    position: relative;
                }

                >.content_button {
                    line-height: 25px;
                    display: inline-block;
                    width: 70%;
                    padding: 10px;
                    position: absolute;
                    bottom: 20px;
                    right: 20px;
                }

                >.ai {
                    border-top-left-radius: 0;
                    background-color: #ffffff !important;
                    color: black !important;
                    align-self: flex-start;
                }



                >.user {
                    border-bottom-right-radius: 0;
                    background-color: #2ecacf;
                    color: #ffffff;
                    align-self: flex-end;
                }
            }


        }

        >.input {
            overflow-y: auto;
            display: flex;
            height: 80px;

            >.left {
                display: flex;
                align-items: flex-end;
                width: calc(100% - 10px);
                padding: 0 10px;
                box-sizing: border-box;

                :deep(.el-textarea__inner) {
                    height: 80px;
                }
            }

            >.right {
                display: flex;
                align-items: flex-end;
                justify-content: flex-end;
                padding: 0 10px 0 0;
                width: 100px;

                button {
                    height: 80px;
                    width: 80px;
                    font-size: 30px;
                    color: #ffffff;
                }
            }
        }

    }

    :deep(.el-dialog) {
        padding: 0 !important;
        border-radius: 10px;
        overflow: hidden;


        .el-dialog__header {
            padding: 0;
        }
    }
}
</style>